package leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Leetcode429 {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };

    public List<List<Integer>> levelOrder(Node root) {
        if(root==null){
            return new ArrayList<List<Integer>>();
        }
        List<List<Integer>> res = new ArrayList<>();
        Queue<Node> q = new LinkedList<Node>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            List<Integer> list= new ArrayList<>();
            while(size--!=0){
                Node tmp = q.poll();
                list.add(tmp.val);
                if(tmp.children!=null){
                    for(int i = 0;i<tmp.children.size();i++){
                        q.offer(tmp.children.get(i));
                    }
                }
            }
            res.add(new ArrayList<>(list));
        }
        return res;
    }
}
